<?php 

	require_once('init.php');
	include('inc/top.php');

	$submitid = sanitizeString($_REQUEST['id']);

	if ($user->data['group_id'] != $mgmtgroup) {
		echo '<div id="main_yellow"><p>You must be an administrator to view this page.</p>';
	} else {

		function steamid($steamid) {
			$id = explode(":", $steamid);
 
			$id[2] = $id[2] * 2;
			$id[2] = $id[2] + $id[1];
			$ro = "765611979";
			$ra = $id[2] + 60265728;
			$final = $ro . $ra;
		
			$url = 'http://steamcommunity.com/profiles/' . $final;
			return $url;
		}

		$submissioninfoq = mysql_query("SELECT * FROM mysql_bans_submission WHERE id='$submitid'");
		$submissioninfoa = mysql_fetch_array($submissioninfoq);

		$bandemosq = mysql_query("SELECT * FROM sm_demos WHERE banid='s$submitid' ORDER BY id ASC");
		$bandemosbool = mysql_num_rows($bandemosq);
		if($bandemosbool) {
			$bandemosa = mysql_fetch_array($bandemosq);
			$bandemosid = $bandemosa[0]['id'];
		}

		$alreadybannedq = mysql_query("SELECT * FROM mysql_bans WHERE steam_id='$submissioninfoa[steamid]' ORDER BY id DESC");
		$alreadybannedbool = mysql_num_rows($alreadybannedq);
		if($alreadybannedbool != 0) { $alreadybanned = true; } else { $alreadybanned = false; }

		if(isset($_POST['deletesubmission'])) {

			mysql_query("DELETE FROM mysql_bans_submission WHERE id='$submitid'") or die(mysql_error());
			redirect('submissions.php');
		}
		if(isset($_POST['addban'])) {

			$input_steamid = sanitizeString($submissioninfoa['steamid']);
			$input_username = sanitizeString($submissioninfoa['username']);
			$input_length = sanitizeString($_REQUEST['input_length']);
			$input_reason = sanitizeString($_REQUEST['input_reason']);
			$input_banned_by = sanitizeString($useradminname);
			$input_banned_by_id = sanitizeString($usersteamid);

			mysql_query("INSERT INTO mysql_bans (steam_id, player_name, ban_length, ban_reason, banned_by, banned_by_id) VALUES ('$input_steamid', '$input_username', '$input_length', '$input_reason', '$input_banned_by', '$input_banned_by_id')") or die(mysql_error());
			$newbanid = mysql_insert_id();
			mysql_query("UPDATE sm_demos SET banid='$newbanid' WHERE banid='s$submitid';");
			mysql_query("DELETE FROM mysql_bans_submission WHERE id='$submitid'") or die(mysql_error());
			echo "<a href='bans.php'><div id='main_green'><p>Ban Added Sucessfully!</p></div></a>";
		} else {

			?>

			<div id="main_gray"><h1>Submission Details</h1></div>

			<?php  if (empty($submitid)) {
				echo "<div id='main_red'><p>No Submission Specified</p></div>";
			} elseif (empty($submissioninfoq)) {
				echo "<div id='main_yellow'><p>Invalid Submission Specified</p></div>";
			} else {
				if($alreadybanned == true) {
					$bancounter=0;
					while($alreadybanneda = mysql_fetch_array($alreadybannedq))
					{
						$bancounter+=1;
						echo "<a href='bandetails.php?banid={$alreadybanneda['id']}'><div id='main_yellow'><p>This user has previously been banned! Click here to view the ban details for ban #$bancounter</p></div></a>";
					}
				}
				?>

				<table class="table" id="tableborder" cellspacing="3" cellpadding="5" align="center" width="500">
					<tr><td align="right"><b>Username:</b></td><td align="left"><?php  echo $submissioninfoa['username']; ?></td></tr>
					<tr><td align="right"><b>SteamID:</b></td><td align="left"><a href="<?php  echo steamid($submissioninfoa['steamid']); ?>"><?php  echo $submissioninfoa['steamid']; ?></a></td></tr>
					<tr><td align="right"><b>Notes:</b></td><td align="left"><?php  echo preg_replace( "#\r\n|\n|\r#", '<br />', $submissioninfoa['notes'] ); ?></td></tr>
					<tr><td align="right"><b>Date/Time:</b></td><td align="left"><?php  echo date("M. j, Y g:i a", strtotime($submissioninfoa['timestamp'])); ?></td></tr>
					<tr><td align="right"><b>IP Address:</b></td><td align="left"><?php  echo $submissioninfoa['ipaddress']; ?></td></tr>
					<?php if($bandemosbool) {  ?>
						<tr><td align="right"><b>Demo:</b></td><td align="left"><a href="getdemo.php?did=<?php echo $bandemosid ?>">Download</a></td></tr>
					<?php } else { ?>
						<tr><td align="right"><b>Demo:</b></td><td align="left">Not uploaded</td></tr>
					<?php } ?>
				</table>

				<table cellspacing="3" cellpadding "5" align="center">

					<tr valign="top">
						<td align="right"><input type="button" name="back" value="Back" class="searchbutton" onClick="javascript:history.back();"></td>
						<td align="left"><form action="submitinfo.php?id=<?php  echo $submitid; ?>" method="post"><input type="submit" name="deletesubmission" value="Delete" class="searchbutton" /></form></td>
					</tr>
				</table>
	
				<form action="submitinfo.php?id=<?php  echo $submitid; ?>" method="post">
					<table cellspacing="3" cellpadding "5" align="center" class="table" id="tableborder">
	
						<tr><td align="left"><b>Reason:</b></td><td><input type="text" name="input_reason" class="searchinput" /></td></tr>
						<tr><td align="left"><b>Length:</b></td><td align="center"> 
							<SELECT NAME="input_length" class="searchinput">
								<option VALUE="0">Permanent</option>
								<option VALUE="1">1 Minute</option>
								<option VALUE="60">1 Hour</option>
								<option VALUE="720">12 Hours</option>
								<option VALUE="1440">1 Day</option>
								<option VALUE="10080">1 Week</option>
								<option VALUE="20160">2 Weeks</option>
								<option VALUE="43200">1 Month</option>
								<option VALUE="259200">6 Months</option>
								<option VALUE="518400">1 Year</option>
						</SELECT></td></tr>
			
						<tr><td colspan="2"><input type="submit" name="addban" value="Ban User" class="searchbutton" /></td></tr>
	
					</table>
				</form>


		<?php 
			}
		}
	}
	
	include('inc/bottom.php');
 ?>